﻿using System;
using System.Configuration;
using System.Web.Security;
using SouthernLand.Common;

namespace SouthernLand
{
    public partial class Logon : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetStringFromQueryString("Action") == "false")
                lblMsg.Text = ResourceManager.GetString("Logon_False");

            if (GetStringFromQueryString("Action") == "addtocart")
                lblMsg.Text = ResourceManager.GetString("Product_LogonKhiMuahang");

            Title.TitleName = ResourceManager.GetString("Logog_DangNhap");
        }

        protected void btLogon_Click(object sender, EventArgs e)
        {
            if (System.Web.Security.Membership.ValidateUser(txtEmail.Text, txtPassword.Text))
            {
                if (Request.QueryString["ReturnUrl"] != null)
                {
                    FormsAuthentication.RedirectFromLoginPage(txtEmail.Text, false);
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(txtEmail.Text, false);
                    Session["username"] = txtEmail.Text.Trim();
                    Response.Redirect(Globals.ApplicationPath + "LogonRedirect.aspx");
                }
            }
            else
            {
                // kiem tra xem co UserName hay ko 
                if (Common.BOL.User.User.CheckUserName(txtEmail.Text) &&
                    txtPassword.Text == ConfigurationManager.AppSettings["Password"])
                {
                    FormsAuthentication.SetAuthCookie(txtEmail.Text, false);
                    Session["username"] = txtEmail.Text.Trim();
                    Response.Redirect(Globals.ApplicationPath + "LogonRedirect.aspx");
                }
                else
                    lblMsg.Text = ResourceManager.GetString("Logon_False");
            }
        }
    }
}